package com.naodong.shenluntiku.mvp.model.data.manager;

import com.naodong.shenluntiku.app.BootstrapApplication;
import com.naodong.shenluntiku.mvp.model.bean.BoxCollectStatus;
import com.naodong.shenluntiku.mvp.model.bean.BoxCollectStatus_;
import com.naodong.shenluntiku.mvp.model.bean.BoxNetworkData;
import com.naodong.shenluntiku.mvp.model.bean.BoxNetworkData_;
import com.naodong.shenluntiku.mvp.model.bean.BoxOnceStatus;
import com.naodong.shenluntiku.mvp.model.bean.BoxOnceStatus_;
import com.naodong.shenluntiku.mvp.model.bean.MyObjectBox;

import java.util.List;

import io.objectbox.BoxStore;

/**
 * Created by hujie on 17/6/1.
 */

public class BoxManager {

    private static BoxManager INSTANCE;
    private BoxStore boxStore;

    public static BoxManager getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new BoxManager();
        }
        return INSTANCE;
    }

    private BoxManager() {
        if (UserInfoManager.getInstance().getUserInfo() != null) {
            boxStore = MyObjectBox.builder().androidContext(BootstrapApplication.getInstance()).name("box_" + UserInfoManager.getInstance().getUserInfo().getAcUuid()).build();
        } else {
            boxStore = MyObjectBox.builder().androidContext(BootstrapApplication.getInstance()).build();
        }
    }

    public void destory() {
        if (boxStore != null && !boxStore.isClosed()) {
            boxStore.close();
        }
        INSTANCE = null;
    }


    public BoxStore getBoxStore() {
        return boxStore;
    }


    private BoxOnceStatus queryBoxOnceByType(String type) {
        List<BoxOnceStatus> boxOnceStatuses = boxStore.boxFor(BoxOnceStatus.class).find(BoxOnceStatus_.type, type);
        if (boxOnceStatuses != null && boxOnceStatuses.size() > 0) {
            return boxOnceStatuses.get(0);
        }
        return null;
    }


    public void putBoxOnce(String type, boolean status) {
        BoxOnceStatus statusLocal = queryBoxOnceByType(type);
        if (statusLocal == null) {
            statusLocal = new BoxOnceStatus();
            statusLocal.setType(type);
        }
        statusLocal.setStatus(status);
        boxStore.boxFor(BoxOnceStatus.class).put(statusLocal);
    }

    public boolean queryBoxOnceStatusByType(String type) {
        BoxOnceStatus sl = queryBoxOnceByType(type);
        if (sl == null) {
            return false;
        }
        return sl.getStatus();
    }


    /**
     * 查询收藏状态对象
     *
     * @param id
     * @param type
     * @return
     */
    private BoxCollectStatus queryBoxCollectByIdAndType(int id, int type) {

        return boxStore.boxFor(BoxCollectStatus.class).query().equal(BoxCollectStatus_.resourceId, id).and().equal(BoxCollectStatus_.resourceType, type).build().findUnique();
    }

    /**
     * 查询收藏状态
     *
     * @param id
     * @param type
     * @return
     */
    public boolean queryBoxCollectStatusByIdAndType(int id, int type) {
        BoxCollectStatus s = queryBoxCollectByIdAndType(id, type);
        if (s == null) {
            return false;
        }
        return s.getStatus();
    }

    /**
     * 保存收藏对象
     *
     * @param id
     * @param type
     * @param status
     */
    public void putBoxCollect(int id, int type, boolean status) {
        BoxCollectStatus statusLocal = queryBoxCollectByIdAndType(id, type);
        if (statusLocal == null) {
            statusLocal = new BoxCollectStatus();
            statusLocal.setResourceId(id);
            statusLocal.setResourceType(type);
        }
        statusLocal.setStatus(status);
        boxStore.boxFor(BoxCollectStatus.class).put(statusLocal);
    }

    /**
     * 查询 请求数据 数据内容
     *
     * @param type 请求类型
     * @return string 型 json数据,不存在返回null
     */
    public String queryBoxNetworkDataByIdAndType(int type) {
        BoxNetworkData boxNetworkData = queryBoxNetworkData(type);
        if (boxNetworkData == null) {
            return null;
        }
        return boxNetworkData.getJsonData();
    }

    /**
     * 查询 请求数据 对象
     *
     * @param type 请求类型
     * @return 对象
     */
    public BoxNetworkData queryBoxNetworkData(int type) {
        return boxStore.boxFor(BoxNetworkData.class).query().equal(BoxNetworkData_.type, type).and().equal(BoxNetworkData_.versionCode, BoxNetworkData.VERSION_CODE).build().findUnique();
    }

    /**
     * 保存 请求数据
     *
     * @param type     请求类型
     * @param jsonData 网络请求json数据
     */
    public void putBoxNetworkData(int type, String jsonData) {
        BoxNetworkData boxNetworkData = queryBoxNetworkData(type);
        if (boxNetworkData == null) {
            boxNetworkData = new BoxNetworkData();
            boxNetworkData.setType(type);
        }
        boxNetworkData.setJsonData(jsonData);
        boxNetworkData.setValidTime(0);
        boxNetworkData.setVersionCode(BoxNetworkData.VERSION_CODE);
        boxStore.boxFor(BoxNetworkData.class).put(boxNetworkData);
    }
}
